package com.itshen.one.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

@Slf4j
@RestController
public class WebFluxController {


    @GetMapping("/mono")
    public Mono<String> testMono() {
        log.info("mono start");
        Mono<String> result = Mono.fromSupplier(() -> "test mono");
        log.info("mono end");
        return result;
    }

    @GetMapping(value = "/flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> testFlux(){
        log.info("flux start");
        Flux<String> result = Flux.fromStream(IntStream.range(1,10).mapToObj(x->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "test flux "+x;
        }));
        log.info("flux end");
        return result;
    }
}
